//
//  MarketSearchViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/1/9.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchResultController.h"
#import "TitleView.h"

@interface SearchViewController () <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) TitleView *titleView;
@property (strong, nonatomic) UISearchBar *searchbar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *searchKeyList;
@property (weak, nonatomic) IBOutlet UIButton *clearbtn;
@property (weak, nonatomic) IBOutlet UIImageView *clearimg;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *blankView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    self.tableView.tableFooterView = blankView;
    
    [self.clearbtn addTarget:self action:@selector(clickClearBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.clearimg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickClearImg:)];
    tap.numberOfTapsRequired = 1;
    [self.clearimg addGestureRecognizer:tap];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] init];
    [backButtonItem setTitle:@""];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    
    UIButton *button = [ViewFactory makeNavigationTextButtonWhite:@"取消"];
    [button addTarget:self action:@selector(goPop) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    self.titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 65, 44)];
    self.titleView.backgroundColor = [UIColor clearColor];
    
    self.searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 7, ScreenWidth - 75, 30)];
    self.searchbar.searchBarStyle = UISearchBarStyleDefault;
    self.searchbar.placeholder = @"搜索你喜欢的部落";
    self.searchbar.layer.masksToBounds = YES;
    self.searchbar.layer.cornerRadius = 4.f;
    self.searchbar.delegate = self;
    [self.titleView addSubview:self.searchbar];
    self.navigationItem.titleView = self.titleView;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.searchbar becomeFirstResponder];
}

- (void)clickClearImg:(UITapGestureRecognizer*)recognizer {
    [self clearSearchList];
}

- (void)clickClearBtn {
    [self clearSearchList];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)goPop {
    [self.searchbar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self addSearchKey:searchBar.text];
    [searchBar resignFirstResponder];
    [self.tableView reloadData];
    SearchResultController *vc = [[SearchResultController alloc] init];
    vc.keywords = searchBar.text;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.searchKeyList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 43;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [ViewFactory makeSearchKeyCell:tableView];
    cell.textLabel.text = [self.searchKeyList objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"searchrecord"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.searchbar resignFirstResponder];
    SearchResultController *vc = [[SearchResultController alloc] init];
    vc.keywords = (NSString *)[self.searchKeyList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - search
- (void)clearSearchList {
    [self.searchKeyList removeAllObjects];
    [UserData setSearchKeyList:self.searchKeyList];
    [self.tableView reloadData];
}

- (void)addSearchKey:(NSString *)key {
    for(NSInteger i = 0; i < self.searchKeyList.count; i++) {
        NSString *str = self.searchKeyList[i];
        if ([key isEqualToString:str]) {
            return;
        }
    }
    
    [self.searchKeyList insertObject:key atIndex:0];
    [UserData setSearchKeyList:self.searchKeyList];
}

- (NSMutableArray *)searchKeyList {
    if (_searchKeyList == nil) {
        _searchKeyList = [UserData searchKeyList];
    }
    return _searchKeyList;
}

@end
